#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "time.h"
#include "CoreGame.h"
#include "Admin.h"
int main()
{
    srand((unsigned)time(NULL));
    Player player; 
    InitPlayer(&player);
    Shore shore;
    InitShore(&shore);
    DynamicArray monster={NULL,10,0};
    InitMonsters(&monster);
    
    DynamicArray treasure={NULL,10,0};
    InitTreasure(&treasure);

    printf("********************\n");
    printf("* 欢迎来到魔塔游戏 *\n");
    printf("********************\n");
    char playername[20];
    GameData(&player,&monster,&treasure,playername);
    while(1)
    {
        for(int i=0;i<monster.len;i++)
        {
            Monster *monsters=(Monster*)monster.dp[i];
            if(monsters->HP>0&&monsters->x==player.x&&monsters->y==player.y)
            {
                if(Battle(&player,monsters)==false)
                {
                    printf("游戏失败\n");
                    return 0;
                }
            }
        }
        for(int i=0;i<treasure.len;i++)
        {
            Treasure *treasures=(Treasure *)treasure.dp[i];
            if(treasures->value!=0&&treasures->x==player.x&&treasures->y==player.y)
            {
                printf("你找到了%s\n",treasures->name.string);
                if(treasures->sign==1)
                    printf("获得了%d金币\n",treasures->value);
                if(treasures->sign==0)
                    printf("增加了%d生命值\n",treasures->value);
                if(treasures->sign==0)
                {
                    player.HP+=treasures->value;
                }
                if(treasures->sign==1)
                {
                    player.monery+=treasures->value;
                }
                treasures->value=0;
            }
        }
        printf("您当前所在的位置是:<%d %d>\n",player.x+1,player.y+1);
        Map(&player);
        printf("请按'w' 's' 'a' 'd'进行移动\n");
        printf("按'q'查看个人属性\n");
        printf("按'p'进入商店\n");
        printf("按'h'保存游戏数据并退出\n");
        char move[1]={0};
        memset(move,0,sizeof(move));
        read(0,move,1);
        int flag=0;
        switch (move[0])
        {
            case 's':
                MakeMove(&player,0,1);
                break;
            case 'w':
                MakeMove(&player,0,-1);
                break;
            case 'a':
                MakeMove(&player,-1,0);
                break;
            case 'd':
                MakeMove(&player,1,0);
                break;
            case 'q':
                system("clear");
                PersonalAttibutes(&player);
                break;
            case 'p':
                system("clear");
                StoreCenter(&shore,&player);
                break;
            case 'h':
                char playerdata[40]={0};
                strcat(playerdata,playername);
                strcat(playerdata,"/player.txt");
                char monsterdata[40]={0};
                strcat(monsterdata,playername);
                strcat(monsterdata,"/monster.txt");
                char treasuredata[40]={0};
                strcat(treasuredata,playername);
                strcat(treasuredata,"/treasure.txt");
                FILE *fd1=fopen(playerdata,"w+");
                FILE *fd2=fopen(monsterdata,"w");
                FILE *fd3=fopen(treasuredata,"w");
                fwrite(&player,sizeof(Player),1,fd1);
                for(int i=0;i<monster.len;i++)
                {
                    Monster *monsters=(Monster*)monster.dp[i];
                    fprintf(fd2,"%s %d %d %d %d",monsters->name.string,monsters->HP,monsters->attack,monsters->x,monsters->y);
                }
                for(int i=0;i<treasure.len;i++)
                {
                    Treasure *treasures=(Treasure *)treasure.dp[i];
                    fprintf(fd3,"%s %d %d %d %d",treasures->name.string,treasures->value,treasures->sign,treasures->x,treasures->y);
                }
                fclose(fd1);
                fclose(fd2);
                fclose(fd3);
                flag=1;
                break;
            default:
                break;
        }
        if(flag==1)
            break;
    }
    for(int i=0;i<monster.len;i++)
    {
        free((Monster*)monster.dp[i]);
    }
    free(monster.dp);
    for(int i=0;i<treasure.len;i++)
    {
        free((Treasure*)treasure.dp[i]);
    }
    free(treasure.dp);
    return 0;
}